datafile = ''
polygonkml = ''
outfilekmz = ''
logofile = 'files/images/phaena.png'
legendfile = 'legend.png'
smallegend = 'smalllegend.png'
#colorMax = 'ff0000'
colorMax = '000000'
colorMin = 'ffffff'


minH = float(10000.0)
maxH = float(40000.0)

def tupColor(hexcolor):
	r = int(hexcolor[0:2],16)
	g = int(hexcolor[2:4],16)
	b = int(hexcolor[4:6],16)
	return r, g, b

def create_legend(title, labeltop='', labelmiddle='', labelbottom=''):
	from PIL import Image, ImageFont, ImageDraw, ImageOps
	
	global smalllegend
	global legendfile
	global colorMax
	global colorMin
	print tupColor(colorMax)
	
	edje = Image.new('RGB', (30, 200))
	imd = ImageDraw.Draw(edje)
	r1, g1, b1 = tupColor(colorMax)
	r2, g2, b2 = tupColor(colorMin)
	print r1, g1, b1
	print r2, g2, b2
	rV = abs(r1 - r2)
	gV = abs(g1 - g2)
	bV = abs(b1 - b2)
	Rstep = float(rV) / 200
	Gstep = float(gV) / 200
	Bstep = float(bV) / 200
	print rV, gV, bV
	#print float(128 /200)
	xR= 0
	xG= 0
	xB= 0
	for l in xrange(200):
		#colour = (r1, g1, b1 * l / 100)
		xR += Rstep
		xG += Gstep
		xB += Bstep
		yR = int(xR)
		yG = int(xG)
		yB = int(xB)
		colour = (yR, yG, yB)
		print colour
		imd.line((0, l, 30, l), fill=colour)
	
	edje.save(smallegend, 'PNG')
	edje.show()

	words = [
	    ((50, 0), labeltop, "#ff0000", 30),
	    ((50, 95), labelmiddle, "#00ff00", 30),
	    ((50, 190), labelbottom, "#0000ff", 30),
	    ]

	# A fully transparent image to work on.
	im = Image.new("RGBA", (100, 300), (0,0,0,0))

	dr = ImageDraw.Draw(im)

	for pos, text, color, size in words:
	    font = ImageFont.load_default()
	    dr.text(pos, text, font=font, fill='White')


	#im.show()
	gradpy = Image.open(smallegend)
	im.paste(gradpy,(12,0))

	dr.rectangle([12,0,40,200], outline='white')

	txt=Image.new('RGBA', (200,9), (0,0,0,0))
	d = ImageDraw.Draw(txt)
	d.text( (0, 0), title,  font=font, fill="White")

	w=txt.rotate(90,  expand=1)
	im.paste(w,(0,0))

	im.save(legendfile, "PNG")


print tupColor('0000A0')
create_legend('edje', 'top', 'mid', 'bot')